package com.example.vbloodbanksalesforce;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.vbloodbanksalesforce.service.HttpUrlConnectionClass;
import com.example.vbloodbanksalesforce.service.Session;

public class DonorRegistration extends BaseActivity {
	EditText dName, dAge, dArea, dPhno, dMailid;
	Spinner sp;
	HttpUrlConnectionClass httpUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.donorentryform);

		dName = (EditText) findViewById(R.id.etdonorname);
		dAge = (EditText) findViewById(R.id.etdonorage);
		dArea = (EditText) findViewById(R.id.etdonorarea);
		dPhno = (EditText) findViewById(R.id.etdonorphno);
		dMailid = (EditText) findViewById(R.id.etdonormailid);

		sp = (Spinner) findViewById(R.id.spbloodgroup);

	}

	public void submitDetails(View v) {

		String name = dName.getText().toString();
		String age = dAge.getText().toString();
		String area = dArea.getText().toString();
		String phno = dPhno.getText().toString();
		String mailid = dMailid.getText().toString();
		String dgroup = sp.getSelectedItem().toString();

		String data[] = { name, dgroup, age, area, phno, mailid };
		if (name.trim().length() == 0) {
			Toast.makeText(getApplicationContext(), "Please enter Name", 1000)
					.show();
		} else if (age.trim().length() == 0) {
			Toast.makeText(getApplicationContext(), "Please enter Age", 1000)
					.show();

		} else if (area.trim().length() == 0) {
			Toast.makeText(getApplicationContext(), "Please enter Area", 1000)
					.show();
		} else if (phno.trim().length() == 0) {
			Toast.makeText(getApplicationContext(), "Please enter Phno", 1000)
					.show();
		} else if (mailid.trim().length() == 0) {
			Toast.makeText(getApplicationContext(), "Please enter Mailid", 1000)
					.show();
		} else {

			httpUrl = new HttpUrlConnectionClass(data);
			httpUrl.attach(this);

			String instanceUrl = Session.sInstanceUrl
					+ "/services/data/v26.0/sobjects/DonorRegistration__c";
			httpUrl.execute(instanceUrl, "insert");
		}

	}
	
	public void resetDetails(View v){
		dName.setText("");
		dAge.setText("");
		dArea.setText("");
		dPhno.setText("");
		dMailid.setText("");
	}

	@Override
	public void apiReturn(String response) {
		// TODO Auto-generated method stub
		
		Log.i("NCS","Registration Response is   :"+response);
		if (response != null) {
			Toast.makeText(getApplicationContext(), "Successfully submitted",
					1000).show();
		}
	}

}
